package cn.itsource.controller.remote;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.itsource.desktop.api.LoginApi;
import cn.itsource.desktop.pojo.param.LoginFound;
import cn.itsource.desktop.pojo.param.LoginUpdate;
import cn.itsource.mapstruct.LoginStructMapper;
import cn.itsource.pojo.domain.Login;
import cn.itsource.result.JSONResult;
import cn.itsource.service.ILoginService;
import cn.itsource.utils.NameUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@RestController
public class LoginApiImpl implements LoginApi {

    @Autowired
    public ILoginService loginService;

    /**
     * 创建一个Login对象
     * @param param ：参数对象
     * @return ：结果
     */
    @Override
    @SaIgnore
    public JSONResult create(LoginFound param) {

        //对象拷贝 : 有弊端
        //BeanUtils.copyProperties(param, login);
        Login login = LoginStructMapper.INSTANCES.loginCreate2Login(param);

        //默认启动
        login.setEnabled(true);
        //默认不是管理员
        login.setAdmin(false);
        //默认头像
        login.setAvatar("https://img1.baidu.com/it/u=2245474832,3720439057&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1718816400&t=41c5128ef0a8a09a8234c7279fb6520b");
        login.setNickName(NameUtil.getName());
        login.setCreateTime(new Date());
        loginService.save(login);

        return JSONResult.success();
    }

    @Override
    public JSONResult update(LoginUpdate param) {
        Login login = loginService.getById(param.getId());
        BeanUtils.copyProperties(param, login);
        login.setUpdateTime(new Date());
        loginService.updateById(login);
        return JSONResult.success();
    }
}
